package com.example.NeteaseCloudMusicApi.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.NeteaseCloudMusicApi.R;
import com.example.NeteaseCloudMusicApi.entity.Banners;
import com.example.NeteaseCloudMusicApi.entity.BannersJson;
import com.example.NeteaseCloudMusicApi.entity.FoundCircleDate;
import com.example.NeteaseCloudMusicApi.entity.FoundCircleDateJson;
import com.example.NeteaseCloudMusicApi.entity.RecommendJson;
import com.example.NeteaseCloudMusicApi.entity.Result;
import com.example.NeteaseCloudMusicApi.ui.APIService;
import com.example.NeteaseCloudMusicApi.ui.ConString;
import com.example.NeteaseCloudMusicApi.ui.adapter.FoundAdapter;
import com.example.NeteaseCloudMusicApi.ui.adapter.RecommendSongListAdapter;
import com.mxn.soul.flowingdrawer_core.FlowingDrawer;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.listener.OnBannerListener;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FoundFragment extends Fragment {
    private static final String TAG = "FoundFragment";

    RecyclerView found_circle_button_RecyclerView,recommend_song_list_RecyclerView;
    View rootView;
    private FlowingDrawer main_id_flowingDrawer;
    private ImageView found_top_image_left;

    Banner banner;
    FoundAdapter foundAdapter;
    RecommendSongListAdapter recommendSongListAdapter;

    private FoundCircleDateJson foundCircleDateJson;
    private BannersJson bannersJson;
    private RecommendJson recommendJson;

    private List<Result> mResult = new ArrayList<>();
    private List<FoundCircleDate> mFoundCircleDate = new ArrayList<>();
    private List<Banners> mBanners = new ArrayList<>();
    List<Integer> list = new ArrayList();

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private TextView textView;
    private ImageView imageView;


    public FoundFragment() {
        // Required empty public constructor

    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MyFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static FoundFragment newInstance(String param1, String param2) {
        FoundFragment fragment = new FoundFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView =  inflater.inflate(R.layout.activity_found, container, false);
        main_id_flowingDrawer = rootView.findViewById(R.id.main_id_flowingDrawer);
        found_top_image_left = rootView.findViewById(R.id.found_top_image_left);
        navigationBar();
        initBanner();
        initFoundCircleButton();
        initRecommend();
        return rootView;
    }

    /*导航栏按钮*/
    public void navigationBar(){
        found_top_image_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                main_id_flowingDrawer.openMenu();
            }
        });
    }

    /*轮播图*/
    private void initBanner(){

//        Retrofit retrofit = new Retrofit.Builder()
//                .baseUrl(ConString.BASE_URL)
//                .addConverterFactory(GsonConverterFactory.create())
//                .build();
//
//        APIService retrofitApi = retrofit.create(APIService.class);
//        Call<BannersJson> userCall = retrofitApi.getBanners();
//        userCall.enqueue(new Callback<BannersJson>() {
//
//            @Override
//            public void onResponse(Call<BannersJson> call, Response<BannersJson> response) {
//                Log.i(TAG,"onResponse:--->"+"轮播图数据请求成功");;
//                bannersJson = response.body();
//                mBanners = bannersJson.getBanners();
////                list.add(banners.get(response).getImageUrl());
//            }
//            @Override
//            public void onFailure(Call<BannersJson> call, Throwable t) {
//                Log.i(TAG,"onFailure:--->"+"轮播图数据请求失败");
//            }
//        });

        list.add(R.mipmap.playlist_1);
        list.add(R.mipmap.playlist_2);
        list.add(R.mipmap.playlist_3);
        list.add(R.mipmap.playlist_4);
        banner = rootView.findViewById(R.id.banner);//寻找组件

        banner.setAdapter(new BannerImageAdapter<Integer>(list) {
            @Override
            public void onBindView(BannerImageHolder holder, Integer data, int position, int size) {
                holder.imageView.setImageResource(data);
            }

        });

        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(Object data, int position) {
                Toast.makeText(getContext() , "点击了第" + (position + 1) + "张轮播图", Toast.LENGTH_SHORT).show();
            }
        });
    }

    /*found页面圆形按钮*/
    private void initFoundCircleButton() {

        /*Found found1 = new Found("每日推荐", R.mipmap.daily_recommend);
        mFound.add(found1);
        Found found2 = new Found("私人FM", R.mipmap.personal_fm);
        mFound.add(found2);
        Found found3 = new Found("歌单", R.mipmap.playlist);
        mFound.add(found3);
        Found found4 = new Found("排行榜", R.mipmap.chart);
        mFound.add(found4);
        Found found5 = new Found("直播", R.mipmap.live_);
        mFound.add(found5);
        Found found6 = new Found("数字专辑", R.mipmap.digital_album);
        mFound.add(found6);
        Found found7 = new Found("唱聊", R.mipmap.sing_chat);
        mFound.add(found7);
        Found found8 = new Found("游戏专区", R.mipmap.game);
        mFound.add(found8);

        recyclerView = rootView.findViewById(R.id.found_circle_button);
        layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new FoundAdapter(mFound);
        recyclerView.setAdapter(adapter);*/

        found_circle_button_RecyclerView = rootView.findViewById(R.id.found_circle_button);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ConString.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        APIService retrofitApi = retrofit.create(APIService.class);
        Call<FoundCircleDateJson> userCall = retrofitApi.getBall();
        userCall.enqueue(new Callback<FoundCircleDateJson>() {

            @Override
            public void onResponse(Call<FoundCircleDateJson> call, Response<FoundCircleDateJson> response) {
                Log.i(TAG,"onResponse:--->"+"found页面圆形按钮数据请求成功");
                System.out.println(response);
                foundCircleDateJson = response.body();
                mFoundCircleDate = foundCircleDateJson.getData();
                //展示
                found_circle_button_RecyclerView.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false));

                //设置适配器
                foundAdapter = new FoundAdapter(getContext(),mFoundCircleDate);
                found_circle_button_RecyclerView.setAdapter(foundAdapter);
            }
            @Override
            public void onFailure(Call<FoundCircleDateJson> call, Throwable t) {
                Log.i(TAG,"onFailure:--->"+"found页面圆形按钮数据请求失败");
            }
        });
    }

    /*推荐歌单*/
    private void initRecommend() {
        recommend_song_list_RecyclerView = rootView.findViewById(R.id.recommend_song_list);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ConString.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        APIService retrofitApi = retrofit.create(APIService.class);
        Call<RecommendJson> userCall = retrofitApi.getRecommend();
        userCall.enqueue(new Callback<RecommendJson>() {
            @Override
            public void onResponse(Call<RecommendJson> call, Response<RecommendJson> response) {
                Log.i(TAG,"onResponse: --->"+"推荐歌单数据请求成功");
                System.out.println(response);
                recommendJson = response.body();
                mResult = recommendJson.getResult();
                //展示
                recommend_song_list_RecyclerView.setLayoutManager(new GridLayoutManager(getContext(),3));

                //设置适配器
                recommendSongListAdapter = new RecommendSongListAdapter(getContext(),mResult);
                recommend_song_list_RecyclerView.setAdapter(recommendSongListAdapter);
            }
            @Override
            public void onFailure(Call<RecommendJson> call, Throwable t) {
                Log.i(TAG,"onFailure: --->"+"推荐歌单数据请求失败");
            }
        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode,
                                 @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 999 && resultCode == 999) {
//            Glide.with(this).load(Account.getId)
        }
    }

}
